<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\UserModel;
use app\portal\validate\AdminUserValidate;
use cmf\controller\AdminBaseController;
use Think\Db;

/**
 * Class AdminIndexController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'门户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'门户管理'
 * )
 */
class AdminIndexController extends AdminBaseController
{


    /**
     *会员列表
     */
    public function index()
    {
        $where = [];
        $param = $this->request->param();
        $param['querytype'] = ['1'=>'用户手机号','2'=>'用户ID','3'=>'用户邮箱'];
        if(!isset($param['type'])){   $param['type'] = 1; }
        if(isset($param['inviter_member_id']) && $param['inviter_member_id']){
            $where['inviter_member_id'] = $param['inviter_member_id'];
        }
        $param['inviter_member_id'] = $inviter_member_id = isset($param['inviter_member_id'])?$param['inviter_member_id'] : '';
        $param['start_time'] = $start_time = isset($param['start_time'])?$param['start_time'] : '';
        $param['end_time'] = $end_time = isset($param['end_time']) ? $param['end_time'] : '';
        $param['keyword'] = $key= isset($param['keyword']) ? $param['keyword'] : '';
        $param['type'] = $type= isset($param['type']) ? $param['type'] : '';

        if(isset($param['keyword']) ){//搜索条件

            if($start_time && $end_time){
                $where['create_time'] = ['between',[strtotime($start_time),strtotime($end_time)]];
            }elseif ($start_time){
                $where['create_time'] = ['>=',strtotime($start_time)];

            }elseif ($end_time){
                $where['create_time'] = ['<=',strtotime($end_time)];
            }

            if($key) {
                if ($type == 2) {
                    $where['id'] = ['like', "%$key%"];
                } elseif ($type == 3) {
                    $where['user_email'] = ['like', "%$key%"];
                } else {
                    $where['user_login'] = ['like', "%$key%"];
                }
            }

        }
        if(isset($param['expUser'])){//导出
            if(isset($param['ids'])){
                $id = $param['ids'];
                $where_pa['id'] = ['in',$id];
            }else{
                $where_pa = $where;
            }
            $this->award_expUser('用户记录表','',$where_pa);
        }
        $user = new UserModel();

        $list = $user->get_user_info_arr($where);//获取用户信息
        //dump($list);

        $this->assign('keyword', $param);//搜索条件
        $this->assign('data',  $list->toArray()['data'] );
        $list->appends($param);//分页保留参数
        $this->assign('page', $list->render());//分页

        return $this->fetch();
    }


    //删除
    public function delete()
    {

        $userid = $this->request->param('id');
        if ( !empty($userid)) {
            $user = new UserModel();
            $res = $user->user_info_delete(['id' => (int)$userid]);
            if ($res == 1) {
                $this->success('删除成功', '');
            }
        }
        $this->error('删除失败', '');

    }


    // 3-列表导出
    public function award_expUser($xlsName='', $xlsCell=array(), $where=''){

        //导出Excel
        if(empty($xlsName)) $xlsName  = "自定义用户导出";
        $xlsCell  = array(
            array('id','用户UID'),

            array('user_login','用户名'),
            array('user_email','用户登录邮箱'),
            array('last_login_ip','最后登录ip'),
            array('create_time','创建时间'),

        );
        $data_list = (new UserModel())->user_info($where);


        if(empty($data_list)){ return; }
        $this->exportExcel($xlsName,$xlsCell,$data_list);
    }

    /**
     * 导出Execl
     * @param $expTitle  表名
     * @param $expCellName 第二行标题
     * @param $expTableData 数据
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     */

    public function exportExcel($expTitle,$expCellName,$expTableData)
    {
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称

        $fileName = $xlsTitle . date('_Ymd_His');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);

        // 引入核心文件
        require  CMF_ROOT.'/simplewind/vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');

        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格
        // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
        for ($i = 0; $i < $cellNum; $i++) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), $expTableData[$i][$expCellName[$j][0]]);
            }
        }

        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }


}
